package collectionexercise02;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SubListDemo {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<10;i++){
            list.add(i*10);
        }
        System.out.println("list原集合："+list);//[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

        List<Integer> sublist=list.subList(3,8);//[ 30, 40, 50, 60, 70]
        System.out.println("sublist:"+sublist);

        //将子集每个元素扩大10倍,原集合相同下标的元素也改变
        for(int i=0;i<sublist.size();i++){
         sublist.set(i,sublist.get(i)*10);
        }
        System.out.println("sublist:"+sublist);//[300, 400, 500, 600, 700]
        System.out.println("list:"+list);//[0, 10, 20, 300, 400, 500, 600, 700, 80, 90]

        //修改原集合的数据的修改，子集合的数据也同时会修改
        list.set(3,50);
        System.out.println("list:"+list);//[0, 10, 20, 50, 400, 500, 600, 700, 80, 90]
        System.out.println("sublist:"+sublist);//[50, 400, 500, 600, 700]

        list.remove(0);
        System.out.println("list:"+list);//[10, 20, 50, 400, 500, 600, 700, 80, 90]
        System.out.println("sublist:"+sublist);//原集合修改长度后，不能再对子集合进行任何操作，否则发生异常，可以重新获取子集




    }
}
